<template>
  <div>
    <!-- TODO -> Style the title and description components so they are the same in every demo-->
    <div id="demo-title-container">
      <h1 id="demo-title">
        <!-- Insert demo title here during demo -->
      </h1>
    </div>
    <div id="demo-description-container">
      <p id="demo-description">
        <!-- Insert demo title here during demo -->
      </p>
    </div>
    <div id="demo-toolbar">
      <!-- Insert buttons/dropdowns/etc here during demo -->
    </div>
    <div id="content"></div>
  </div>
</template>

<script>
import {
  RenderingEngine,
  Types,
  Enums,
  volumeLoader,
  getRenderingEngine,
} from '@cornerstonejs/core';

import initDemo from '@/assets/utils/demo/helpers/initDemo'
import createImageIdsAndCacheMetaData from '@/assets/utils/demo/helpers/createImageIdsAndCacheMetaData'
import addButtonToToolbar from '@/assets/utils/demo/helpers/addButtonToToolbar';
import addDropdownToToolbar from "@/assets/utils/demo/helpers/addDropdownToToolbar";
import setCtTransferFunctionForVolumeActor from '@/assets/utils/demo/helpers/setCtTransferFunctionForVolumeActor'
import setPetColorMapTransferFunctionForVolumeActor from '@/assets/utils/demo/helpers/setPetColorMapTransferFunctionForVolumeActor'
import {onMounted} from "vue";
import addSliderToToolbar from "../assets/utils/demo/helpers/addSliderToToolbar";
import * as cornerstoneTools from "@cornerstonejs/tools";
import {segmentation, SegmentationDisplayTool, ToolGroupManager} from "@cornerstonejs/tools";

export default {
  name: "OHIFViewer",
  setup(){
    const { ViewportType } = Enums;
    const renderingEngineId = 'myRenderingEngine';
    const viewportId = 'CT_SAGITTAL_STACK';

// Define unique ids for the volumes
    const volumeLoaderScheme = 'cornerstoneStreamingImageVolume'; // Loader id which defines which volume loader to use
    const ctVolumeName = 'CT_VOLUME_ID'; // Id of the volume less loader prefix
    const ctVolumeId = `${volumeLoaderScheme}:${ctVolumeName}`; // VolumeId with loader id + volume id

// Define a unique id for the volume
    const ptVolumeName = 'PT_VOLUME_ID';
    const ptVolumeId = `${volumeLoaderScheme}:${ptVolumeName}`;

    const {
      CrosshairsTool,
      ToolGroupManager,
      StackScrollMouseWheelTool,
      Enums: csToolsEnums,
      segmentation,
      utilities: cstUtils,
      SegmentationDisplayTool,
      RectangleScissorsTool,
      SphereScissorsTool,
      CircleScissorsTool,
      BrushTool,
      PaintFillTool,
      PanTool,
      ZoomTool,
      WindowLevelTool,
    } = cornerstoneTools;

    onMounted(()=>{


      addDropdownToToolbar({
        options: {
          values: ['axial', 'sagittal', 'coronal', 'oblique'],
          defaultValue: 'sagittal'
        },
        onSelectedValueChange: function onSelectedValueChange(selectedValue) {
          // Get the rendering engine
          const renderingEngine = getRenderingEngine(renderingEngineId);

          // Get the volume viewport
          const viewport = renderingEngine.getViewport(viewportId);

          let viewUp = void 0;
          let viewPlaneNormal = void 0;

          switch (selectedValue) {
            case orientationOptions.axial:
              viewport.setOrientation(Enums.OrientationAxis.AXIAL);
              break;
            case orientationOptions.sagittal:
              viewport.setOrientation(Enums.OrientationAxis.SAGITTAL);
              break;
            case orientationOptions.coronal:
              viewport.setOrientation(Enums.OrientationAxis.CORONAL);
              break;
            case orientationOptions.oblique:
              // Some random oblique value for this dataset
              viewUp = [-0.5962687530844388, 0.5453181550345819, -0.5891448751239446];
              viewPlaneNormal = [-0.5962687530844388, 0.5453181550345819, -0.5891448751239446];

              viewport.setCamera({ viewUp: viewUp, viewPlaneNormal: viewPlaneNormal });
              viewport.resetCamera();
              break;
          }

          viewport.render();
        }
      });

      run();
    })

    async function pt() {
      const ptImageIds = [
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000012.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000017.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000001.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000010.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000018.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000015.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000008.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000006.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000002.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000023.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000003.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000014.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000009.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000007.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000000.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000000.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000024.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000016.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000011.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000019.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000025.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000021.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000020.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000004.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000013.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000022.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000005.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000039.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000038.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000026.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000034.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000031.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000032.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000033.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000041.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000030.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000045.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000046.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000040.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000043.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000048.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000047.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000027.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000050.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000049.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000042.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000029.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000035.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000037.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000052.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000044.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000036.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000028.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000051.dcm",
      ]

      const ptVolume = await volumeLoader.createAndCacheVolume(ptVolumeId, {
        imageIds: ptImageIds,
      });


      // Set the volume to load
      ptVolume.load();
    }

    async function run() {
      const content = document.getElementById('content');
      const element = document.createElement('div');
      element.id = 'cornerstone-element';
      element.style.width = '500px';
      element.style.height = '500px';
      content.appendChild(element);

      let fused = false;

      addButtonToToolbar({
        title: 'toggle PET',
        onClick: function onClick() {
          // Get the rendering engine
          const renderingEngine = getRenderingEngine(renderingEngineId);

          // Get the volume viewport
          const viewport = renderingEngine.getViewport(viewportId);
          if (fused) {
            // Removes the PT actor from the scene
            viewport.removeVolumeActors([ptVolumeId], true);

            fused = false;
          } else {
            // Add the PET volume to the viewport. It is in the same DICOM Frame Of Reference/worldspace
            // If it was in a different frame of reference, you would need to register it first.
            viewport.addVolumes(
                [
                  {
                    volumeId: ptVolumeId,
                    callback: setPetColorMapTransferFunctionForVolumeActor,
                  },
                ],
                true
            );

            fused = true;
          }
        }
      });


     await initDemo();

      const wadoRsRoot = 'https://d3t6nz73ql33tx.cloudfront.net/dicomweb';
      const StudyInstanceUID =
          '1.3.6.1.4.1.14519.5.2.1.7009.2403.334240657131972136850343327463';

      // Get Cornerstone imageIds and fetch metadata into RAM
      const ctImageIds = [
        "wadouri:http://localhost:8080/webPage/download/dcm/CT000012.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000017.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000001.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000010.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000018.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000015.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000008.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000006.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000002.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000023.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000003.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000014.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000009.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000007.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000000.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000000.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000024.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000016.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000011.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000019.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000025.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000021.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000020.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000004.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000013.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000022.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000005.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000039.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000038.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000026.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000034.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000031.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000032.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000033.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000041.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000030.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000045.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000046.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000040.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000043.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000048.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000047.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000027.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000050.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000049.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000042.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000029.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000035.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000037.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000052.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000044.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000036.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000028.dcm",
        "wadouri:https://plantarflex.github.io/cornerstoneTools-SyncBrushTool/data/CT000051.dcm",
      ]
      // const ptImageIds = await createImageIdsAndCacheMetaData({
      //   StudyInstanceUID,
      //   SeriesInstanceUID:
      //         '1.3.6.1.4.1.14519.5.2.1.7009.2403.226151125820845824875394858561',
      //       // '1.3.6.1.4.1.14519.5.2.1.7009.2403.879445243400782656317561081015',
      //   wadoRsRoot,
      // });

      console.log(ctImageIds)

      // Instantiate a rendering engine
      const renderingEngine = new RenderingEngine(renderingEngineId);

      const viewportInput = {
        viewportId: viewportId,
        type: ViewportType.ORTHOGRAPHIC,
        element: element,
        defaultOptions: {
          orientation: Enums.OrientationAxis.AXIAL,
          background: [0.2, 0, 0.2],
        },
      };

      renderingEngine.enableElement(viewportInput);

      const viewport = renderingEngine.getViewport(viewportId);

      const ctVolume = await volumeLoader.createAndCacheVolume(ctVolumeId, {
        imageIds: ctImageIds,
      });

      ctVolume.load();

      await viewport.setVolumes([
        {volumeId: ctVolumeId, callback: setCtTransferFunctionForVolumeActor},
      ]);

      renderingEngine.render();

      await pt()
    }
  }
}
</script>

<style scoped>

</style>